﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SharingCenter.Web.Classes.Favorites
{
    using SharingCenter.Web.Classes;
    public static class FavoriteOperations
    {
        public static Sc_SharedFavorite GetFavorite(string contextName, Guid contextId, Guid userId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    return cxt.Sc_SharedFavorites.Single(d => (d.UserId == userId && contextId == d.ContextId && contextName == d.ContextName));
                }
                catch { return null; }
            }
        }

        public static bool FavoriteExists(string contextName, Guid contextId, Guid userId)
        {
            return GetFavorite(contextName, contextId, userId) != null;
        }

        public static Guid AddFavorite(string contextName, Guid contextId, Guid userId, string comment)
        {
            if (!FavoriteExists(contextName, contextId, userId))
            {
                Sc_SharedFavorite fav = new Sc_SharedFavorite();
                fav.FavoriteId = Guid.NewGuid();
                fav.ContextId = contextId;
                fav.ContextName = contextName;
                fav.Comment = comment;
                fav.UserId = userId;
                fav.CreateDate = DateTime.Now;

                scDataContext.Instance.Sc_SharedFavorites.InsertOnSubmit(fav);
                scDataContext.Instance.SubmitChanges();
                return fav.FavoriteId;
            }
            else
            {
                return Guid.Empty;
            }
        }

        public static bool DeleteFavorite(string contextName, Guid contextId, Guid userId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                Sc_SharedFavorite fav = cxt.Sc_SharedFavorites.SingleOrDefault(d => (d.UserId == userId && contextId == d.ContextId && contextName == d.ContextName));
                if (fav != null)
                {
                    cxt.Sc_SharedFavorites.DeleteOnSubmit(fav);
                    cxt.SubmitChanges();
                }

                return true;
            }
        }

        public static List<Guid> GetFavoriteCollectorUsers(string contextName, Guid contextId, int count)
        {
            var q = new List<Guid>();
            using (scDataContext cxt = new scDataContext())
            {
                if (count > 0)
                {
                    q = (from t in cxt.Sc_SharedFavorites
                         where t.ContextName == contextName && t.ContextId == contextId
                         orderby t.CreateDate descending
                         select t.UserId).Take(count).ToList();
                }
                else
                {
                    q = (from t in cxt.Sc_SharedFavorites
                         where t.ContextName == contextName && t.ContextId == contextId
                         orderby t.CreateDate descending
                         select t.UserId).ToList();
                }
            }

            return q;
        }
    }
}

